home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / bisonpcb / files.c < prev    next >
Text File  |  1987-02-13  |  6KB  |  253 lines

  1. /* Open and close files for bison,
  2.    Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3.   
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.   
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.   
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. /*
  22.  * Port to PC by Whit Gregg
  23.  *               Nourse, Gregg & Browne, Inc.
  24.  *         1 Horizon Road
  25.  *         Fort Lee, NJ  07024
  26.  */
  27.  
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>    /* WG */
  31. #include <string.h>    /* WG */
  32. #include "files.h"
  33. #include "new.h"
  34. #include "gram.h"
  35. #include "func.h"    /* WG */
  36.  
  37.     FILE * finput = NULL;
  38. FILE *foutput = NULL;
  39. FILE *fdefines = NULL;
  40. FILE *ftable = NULL;
  41. FILE *fattrs = NULL;
  42. FILE *fguard = NULL;
  43. FILE *faction = NULL;
  44. FILE *fparser = NULL;
  45.  
  46. /* FILE *fparser1 = NULL; JF we don't need this anymore */
  47.  
  48. char *infile;
  49. char *outfile;
  50. char *defsfile;
  51. char *tabfile;
  52. char *attrsfile;
  53. char *guardfile;
  54. char *actfile;
  55. char *tmpattrsfile;
  56. char *tmptabfile;
  57.  
  58. /* JF nobody really uses these */
  59. /* char *pfile;
  60. char *pfile1; */
  61.  
  62. char *mktemp();            /* So the compiler won't complain */
  63. FILE *tryopen();        /* This might be a good idea */
  64.  
  65. extern int verboseflag;
  66. extern int definesflag;
  67. int fixed_outfiles = 0;
  68.  
  69.  
  70.  
  71. char *
  72. stringappend(string1, end1, string2)
  73.     char *string1;
  74.     int end1;
  75.     char *string2;
  76. {
  77.     register char *ostring;
  78.     register char *cp, *cp1;
  79.     register int i;
  80.  
  81.     cp = string2;
  82.     i = 0;
  83.     while (*cp++)
  84.         i++;
  85.  
  86.     ostring = NEW2(i + end1 + 1, char);
  87.  
  88.     cp = ostring;
  89.     cp1 = string1;
  90.     for (i = 0; i < end1; i++)
  91.         *cp++ = *cp1++;
  92.  
  93.     cp1 = string2;
  94.     while (*cp++ = *cp1++);
  95.  
  96.     return ostring;
  97.     }
  98.  
  99.  
  100. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  101.    the output files, and opens the tmp files and the parser */
  102. void 
  103. openfiles()
  104. {                /* WG */
  105.     char *name_base;
  106.     register int i;
  107.     register char *cp;
  108.     char *tmp_dir;        /* WG */
  109.     char stemp[64];        /* WG */
  110.  
  111.     name_base = fixed_outfiles ? "yyy" : infile;
  112.     tmp_dir = getenv("TMP");/* WG */
  113.     if (tmp_dir == NULL)
  114.         tmp_dir = "";    /* WG */
  115.  
  116.     cp = name_base;
  117.     i = 0;
  118.     while (*cp++)
  119.         i++;
  120.  
  121.     cp -= 2;
  122.  
  123.     if (*cp-- == 'y' && *cp == '.')
  124.         i -= 2;
  125.  
  126.  
  127.     finput = tryopen(infile, "r");
  128.  
  129.     fparser = tryopen(PFILE1, "r");
  130.  
  131.     if (verboseflag) {
  132.         outfile = stringappend(name_base, i,OUTPUT_EXTENSION);    /* WG */
  133.         foutput = tryopen(outfile, "w");
  134.         }
  135.  
  136.     if (definesflag) {
  137.         defsfile = stringappend(name_base,i,DEFINES_EXTENSION);    /* WG */
  138.         fdefines = tryopen(defsfile, "w");
  139.         }
  140.  
  141.     strcpy(stemp, tmp_dir);    /* WG */
  142.     strcat(stemp, "acXXXXXX");    /* WG */
  143.     actfile = mktemp(stemp);/* WG */
  144.     faction = tryopen(actfile, "w+");
  145.     unlink(actfile);
  146.  
  147.     strcpy(stemp, tmp_dir);    /* WG */
  148.     strcat(stemp, "atXXXXXX");    /* WG */
  149.     tmpattrsfile = mktemp(stemp);    /* WG */
  150.     fattrs = tryopen(tmpattrsfile, "w+");
  151.     unlink(tmpattrsfile);
  152.  
  153.     strcpy(stemp, tmp_dir);    /* WG */
  154.     strcat(stemp, "tbXXXXXX");    /* WG */
  155.     tmptabfile = mktemp(stemp);    /* WG */
  156.     ftable = tryopen(tmptabfile, "w+");
  157.     unlink(tmptabfile);
  158.     tabfile = stringappend(name_base, i,TABLE_EXTENSION);    /* WG */
  159.  
  160.     /* These are opened by open_extra_files, if at all */
  161.     attrsfile = stringappend(name_base, i,ATTRIBUTE_EXTENSION);    /* WG */
  162.     guardfile = stringappend(name_base, i,GUARD_EXTENSION);    /* WG */
  163.  
  164.     }
  165.  
  166.  
  167.  
  168. /* open the output files needed only for the semantic parser.
  169. This is done when %semantic_parser is seen in the declarations section.  */
  170. void 
  171. open_extra_files()
  172. {                /* WG */
  173.     FILE *ftmp;
  174.     int c;
  175.  
  176.     /* JF change open parser file */
  177.     fclose(fparser);
  178.     fparser = tryopen(PFILE, "r");
  179.  
  180.     /* JF change from inline attrs file to separate one */
  181.     ftmp = tryopen(attrsfile, "w");
  182.     rewind(fattrs);
  183.     while ((c = getc(fattrs)) != EOF)    /* Thank god for buffering */
  184.         putc((char) c, ftmp);    /* WG */
  185.     fclose(fattrs);
  186.     fattrs = ftmp;
  187.  
  188.     fguard = tryopen(guardfile, "w");
  189.  
  190.     }
  191.  
  192.  /*
  193.   * JF to make file opening easier.  This func tries to open file NAME with
  194.   * mode MODE, and prints an error message if it fails. 
  195.   */
  196. FILE *
  197. tryopen(name, mode)
  198.     char *name;
  199.     char *mode;
  200. {
  201.     FILE *ptr;
  202.  
  203.     ptr = fopen(name, mode);
  204.     if (ptr == NULL) {
  205.         fprintf(stderr, "bison: ");
  206.         perror(name);
  207.         done(2);
  208.         }
  209.     return ptr;
  210.     }
  211.  
  212. void 
  213. done(k)                /* WG */
  214.     int k;
  215. {
  216.     if (faction)
  217.         fclose(faction);
  218.  
  219.     if (fattrs)
  220.         fclose(fattrs);
  221.  
  222.     if (fguard)
  223.         fclose(fguard);
  224.  
  225.     if (finput)
  226.         fclose(finput);
  227.  
  228.     if (fparser)
  229.         fclose(fparser);
  230.  
  231. /* JF we don't need this anymore
  232.   if (fparser1)
  233.     fclose(fparser1); */
  234.  
  235.     if (foutput)
  236.         fclose(foutput);
  237.  
  238.     /* JF write out the output file */
  239.     if (k == 0 && ftable) {
  240.         FILE *ftmp;
  241.         register int c;
  242.  
  243.         ftmp = tryopen(tabfile, "w");
  244.         rewind(ftable);
  245.         while ((c = getc(ftable)) != EOF)
  246.             putc((char) c, ftmp);    /* WG */
  247.         fclose(ftmp);
  248.         fclose(ftable);
  249.         }
  250.  
  251.     exit(k);
  252.     }
  253.